package com.PengGoBook.main.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.PengGoBook.main.BooksCategoryListEntity;
import com.PengGoBook.main.R;
import com.PengGoBook.main.biz.NewsDao;
import com.PengGoBook.main.entity.BookItem;
import com.PengGoBook.main.entity.BooksContentItem;
import com.PengGoBook.main.entity.BooksMoreResponse;
import com.PengGoBook.main.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-5-29
 * Time: 上午10:09
 * To change this template use File | Settings | File Templates.
 */
@SuppressLint("NewApi")
public class BooksFragment extends BaseListFragment {

    public Activity mActivity;
    private List<BooksContentItem> items_list = new ArrayList<BooksContentItem>();
    private String more_url;
    private MyAdapter mAdapter;
    private BooksCategoryListEntity loadMoreEntity;

    // private DisplayImageOptions options;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
//                    more_url = loadMoreEntity.getMore_url();
                    mAdapter.appendToList(loadMoreEntity.getCollections());
                    break;
            }
//            onLoad();
        }

    };

    // add this constructor by King0769, 2013/5/7
    // in order to solve an exception that "can't instantiate class BooksFragment; no empty constructor"
    // I found it in this case : 1.open eoe program -> 2.change system language -> 3.reopen eoe, can see FC(force close)
    // I think this bug will happens in many cases.
    public BooksFragment() {

    }
    //--------------------

    public BooksFragment(Activity c, BooksCategoryListEntity categorys) {
        this.mActivity = c;

        if (categorys != null) {
            this.items_list = categorys.getCollections();
//            more_url = categorys.getMore_url();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreateView(inflater, container, savedInstanceState);

//        listview.setXListViewListener(this);
        // construct the RelativeLayout
        mAdapter = new MyAdapter();
        mAdapter.appendToList(items_list);
        gridView.setAdapter(mAdapter);
       /* listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                BooksContentItem item = (BooksContentItem) mAdapter
                        .getItem(position - 1);
                startDetailActivity(mActivity, item.getDetail_url(), "资讯",
                        item.getTitle());
            }
        });*/
        return view;
    }

    class MyAdapter extends BaseAdapter {
        List<BooksContentItem> mList = new ArrayList<BooksContentItem>();

        public MyAdapter() {
        }

        public void appendToList(List<BooksContentItem> lists) {
            if (lists == null) {
                return;
            }
            mList.addAll(lists);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            BooksContentItem item = mList.get(position);
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater
                        .inflate(R.layout.news_item_layout, null);
                holder.title_ = (TextView) convertView
                        .findViewById(R.id.news_title);
                holder.img_thu = (ImageView) convertView
                        .findViewById(R.id.img_thu);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();

            }
            holder.title_.setText(item.getBook().getTitle());
            String img_url = item.getBook().getImage();
            if (img_url.equals(null) || img_url.equals("")) {
                Log.i("info_out","图片消失");
                holder.img_thu.setVisibility(View.GONE);
            } else {
                Log.i("info_out","图片加载");
                holder.img_thu.setVisibility(View.VISIBLE);
                ImageUtil.setThumbnailView(img_url, holder.img_thu, mActivity,
                        callback1, false);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        public TextView title_;
        public ImageView img_thu;
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
//        onLoad();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        if (more_url.equals(null) || more_url.equals("")) {
            mHandler.sendEmptyMessage(1);
            return;
        } else {

           /* new Thread() {

                @Override
                public void run() {
                    BooksMoreResponse response = new NewsDao(mActivity)
                            .getMore(more_url);
                    if (response != null) {
                        loadMoreEntity = response.getResponse();
                        mHandler.sendEmptyMessage(0);
                    }
                    super.run();
                }
            }.start();*/

        }

    }

}